/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.abc;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import nextapp.echo2.app.*;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.table.DefaultTableModel;
import org.sapra.core.DBManager;
import org.sapra.dao.Practicante;
import org.sapra.echo.Helpers;
import org.sapra.echo.wAviso;
import org.sapra.main.vPrincipal;

/**
 *
 * @author luiz
 */
public class vGlobalSearch extends ContentPane{
    private vPrincipal princ;
    public vGlobalSearch(vPrincipal princ) {
        super();
        if(princ != null)this.princ = princ;
        initComponents();
    }
    private CheckBox nom_ch,pat_ch,mat_ch;
    private TextField nom_txt,pat_txt,mat_txt;
    private Button buscar;
    private SplitPane sp,sp2;
    private Table pract_tb;
    private int led1=0,led2=0,led3=0;
    public void initComponents(){
            //sp=Helpers.SplitPaneHelper(SplitPane.ORIENTATION_HORIZONTAL, new ExtentEx(250,ExtentEx.PX), false);
            //sp2=Helpers.SplitPaneHelper(SplitPane.ORIENTATION_HORIZONTAL, new ExtentEx(200,ExtentEx.PX), false);
            //sp.add(sp2);

            Column col1=new Column();
            Row menu=princ.getMenu();

            
            col1.add(menu);
            Row linea1=Helpers.Row("Default", new Extent(5), col1);
            nom_ch=Helpers.CheckBox("", linea1);
            Helpers.Label("NOMBRE:", Color.BLACK, linea1);
            nom_txt = Helpers.Text("", linea1);
            nom_txt.setMaximumLength(30);nom_txt.setWidth(new Extent(120));


            Row linea2=Helpers.Row("Default", new Extent(5), col1);
            pat_ch=Helpers.CheckBox("", linea2);
            Helpers.Label("PATERNO:", Color.BLACK, linea2);
            pat_txt = Helpers.Text("", linea2);
            pat_txt.setMaximumLength(30);pat_txt.setWidth(new Extent(120));

            Row linea3=Helpers.Row("Default", new Extent(5), col1);
            mat_ch=Helpers.CheckBox("", linea3);
            Helpers.Label("MATERNO:", Color.BLACK, linea3);
            mat_txt = Helpers.Text("", linea3);
            mat_txt.setMaximumLength(30);pat_txt.setWidth(new Extent(120));
            
            buscar=Helpers.Button("BUSCAR", col1, null);
            buscar.setWidth(new Extent(100));
            
            Row master=new Row();
            master.add(col1);
            master.setAlignment(Alignment.ALIGN_CENTER);
            add(master);
            pract_tb=Helpers.Tabla();
            col1.add(pract_tb);
            action();
            inicializar();
    }

    private List<Practicante> lista=null;
    public void action(){
        nom_ch.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(nom_ch.isSelected()){
                    led1=10;
                    nom_txt.setEnabled(true);
                }else{
                    led1=0;
                    nom_txt.setEnabled(false);
                }
            }
        });
        pat_ch.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(pat_ch.isSelected()){
                    led2=100;
                    pat_txt.setEnabled(true);
                }else{
                    led2=0;
                    pat_txt.setEnabled(false);
                }
            }
        });
        mat_ch.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    if(mat_ch.isSelected()){
                        led3=1000;
                        mat_txt.setEnabled(true);
                    }else{
                        led3=0;
                        mat_txt.setEnabled(false);
                    }
                }
            });
        buscar.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    int sumaLed=led1+led2+led3;//suma 1110;
                    String cad="";
                    if(led1==10){
                        if(nom_txt.getText().trim().equals("")){
                            add(new wAviso("ERROR","FAVOR DE ESCRIBIR EL NOMBRE"));
                                    return;
                        }

                    }
                    if(led2==100){
                        if(pat_txt.getText().trim().equals("")){
                            add(new wAviso("ERROR","FAVOR DE ESCRIBIR EL APELLIDO PATERNO"));
                                    return;
                        }

                    }
                    if(led3==1000){
                        if(mat_txt.getText().trim().equals("")){
                            add(new wAviso("ERROR","FAVOR DE ESCRIBIR EL APELLIDO MATERNO"));
                                    return;
                        }

                    }
                    if(sumaLed != 0){
                        if(sumaLed == 1110){//las 3
                            lista=busqueda(nom_txt.getText().trim(),pat_txt.getText().trim(),mat_txt.getText().trim());
                        }
                        if(sumaLed == 1100){ // led 2 y 3
                            lista=busqueda(null,pat_txt.getText().trim(),mat_txt.getText().trim());
                        }
                        if(sumaLed == 1010){//led 1 y 3
                            lista=busqueda(nom_txt.getText().trim(),null,mat_txt.getText().trim());
                        }
                        if(sumaLed ==110){ //led1 y 2
                            lista=busqueda(nom_txt.getText().trim(),pat_txt.getText().trim(),null);
                        }
                        if(sumaLed == 1000){//led 3
                            lista=busqueda(null,null,mat_txt.getText().trim());
                        }
                        if(sumaLed == 100){//led2
                            lista=busqueda(null,pat_txt.getText().trim(),null);
                        }
                        if(sumaLed == 10){//led1
                            lista=busqueda(nom_txt.getText().trim(),null,null);
                        }
                        if(lista !=null )
                            pract_tb.setModel(modeloTabla());
                        else{
                            add(new wAviso("ERROR","NO EXISTEN PRACTICANTES CON ESA DESCRIPCION"));
                                    return;
                        }

                    }

                }
            });
        }
     private String title[]={"Id","Nombre Completo","Empresa","Fecha Inicio","Fecha Termino","Fecha Cancelacion"};
    public void inicializar(){
        nom_txt.setEnabled(false);
        pat_txt.setEnabled(false);
        mat_txt.setEnabled(false);
        pract_tb.setWidth(new Extent(97,Extent.PERCENT));
        DefaultTableModel dtm=new DefaultTableModel(6,0);
        for(int i=0;i<title.length;i++){
                dtm.setColumnName(i,title[i]);
            }
        pract_tb.setModel(dtm);
    }

    static List<Practicante> busqueda(String txt1,String txt2,String txt3){
        List<Practicante> resp = null;
        String sql = "SELECT p.practicanteId,p.nombre,p.paterno,p.materno,"
                + " p.fec_inicioContrato,p.fec_terminoContrato,e.razon_empresa"
                + " FROM practicante p "
                + " INNER JOIN departamento d"
                + " ON d.departamentoId=p.fk_departamentoId"
                + " INNER JOIN empresa e"
                + " ON d.fk_empresaId=e.empresaId "
                + " WHERE ";
                if(txt1 != null){
                    sql+=" ( p.nombre LIKE '"+txt1+"%' OR p.nombre LIKE '%"+txt1+"%' OR p.nombre LIKE '%"+txt1+"')";
                    if(txt2 != null || txt3 !=null)
                        sql += " AND ";
                }

                if(txt2 != null){
                    sql+=" ( p.paterno LIKE '"+txt2+"%' OR p.paterno LIKE '%"+txt2+"%' OR p.paterno LIKE '%"+txt2+"')";
                    if( txt3 !=null)
                        sql += " AND ";
                }

                if(txt3 != null){
                    sql+=" ( p.materno LIKE '"+txt3+"%' OR p.materno LIKE '%"+txt3+"%' OR p.materno LIKE '%"+txt3+"')";
                }

                sql+=" ORDER BY e.razon_empresa,p.nombre ASC";
                //System.out.println(sql);
        try {
            resp = DBManager.getQuery(Practicante.class,sql);
        } catch (Exception e) {
            Logger.getLogger(dPracticante.class.getName()).log(Level.SEVERE, null, e);
            return null;
        }
        return resp;
    }

    public DefaultTableModel modeloTabla(){
            SimpleDateFormat spd=new SimpleDateFormat("yyyy-MM-dd");
            DefaultTableModel dtm=new DefaultTableModel(6,0);
            for(int i=0;i<title.length;i++){
                    dtm.setColumnName(i,title[i]);
                }
            for(int i=0;i<lista.size();i++){
                    Practicante p=(Practicante)lista.get(i);
                    Object[] obj=new Object[6];
                    obj[0]=p.getPracticanteId()+"";
                    obj[1]=p.getNombre()+" "+p.getPaterno()+" "+p.getMaterno();
                    obj[2]=p.getRazon_empresa();
                    obj[3]=spd.format(p.getFec_inicioContrato());
                    obj[4]=spd.format(p.getFec_terminoContrato());
                    if(p.getFec_cancelacion()==null || p.getFec_cancelacion().equals(""))
                        obj[5]="N/D";
                    else
                        obj[5]=spd.format(p.getFec_cancelacion());
                    dtm.addRow(obj);
            }
            return dtm;
    }
}
